<% layout('/layouts/default.html', {title: '营销活动管理', libs: ['validate','dataGrid','fileupload']}){ %>
<div class="main-content">
	<div class="box box-main">
		<div class="box-header with-border">
			<div class="box-title">
				<i class="fa icon-note"></i> ${text(activity.isNewRecord ? '新增营销活动' : '编辑营销活动')}
			</div>
			<div class="box-tools pull-right">
				<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
			</div>
		</div>
		<#form:form id="inputForm" model="${activity}" action="${ctx}/storage/activity/save" method="post" class="form-horizontal">
		<div class="box-body">
			<div class="form-unit">${text('基本信息')}</div>
			<#form:hidden path="id"/>
			<#form:hidden path="type"/>
			<div class="row" style="${(parameter.type == '限时秒杀' ? 'display:none;' : '')}">
				<div class="col-xs-12">
					<div class="form-group">
						<label class="control-label col-sm-2"><span class="required ">*</span> ${text('活动主图')}：</label>
						<div class="col-sm-10">
							<div style="color:red;margin-bottom:10px;">
								建议尺寸：981PX * 520PX
							</div>
							<#form:hidden class="form-control" id="thumbnail" name="thumbnail" path="thumbnail"/>
							<#form:fileupload id="imagethumbnail" uploadType="image" returnPath="true" filePathInputId="thumbnail" class="form-control" readonly="false" maxUploadNum="1" imageMaxWidth="981" imageMaxHeight="520"/>
						</div>
					</div>
				</div>
			</div>
			<div class="row">
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required ">*</span> ${text('顺序')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:input path="sort" maxlength="255" class="form-control required digits"/>
						</div>
					</div>
				</div>
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required ">*</span> ${text('名称')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:input path="name" maxlength="255" class="form-control required"/>
						</div>
					</div>
				</div>
			</div>
			<div class="row" style="${(parameter.type == '限时秒杀' ? 'display:none;' : '')}">
				<div class="col-xs-12">
					<div class="form-group">
						<label class="control-label col-sm-2" title="">
							<span class="required ">*</span> ${text('品牌')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-10">
							<#form:select path="brandId" readonly="${!activity.isNewRecord}" class="form-control" items="${brands}" itemLabel="name" itemValue="id" blankOption="true" blankOptionLabel="请选择"/>
						</div>
					</div>
				</div>
			</div>
			<div class="row">
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required ">*</span> ${text('开始时间')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:input path="startTime" readonly="true" maxlength="20" class="form-control Wdate required"
							dataFormat="datetime" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm',isShowClear:false});"/>
						</div>
					</div>
				</div>
				<div class="col-xs-6">
					<div class="form-group">
						<label class="control-label col-sm-4" title="">
							<span class="required ">*</span> ${text('结束时间')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-8">
							<#form:input path="endTime" readonly="true" maxlength="20" class="form-control Wdate required"
							dataFormat="datetime" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm',isShowClear:false});"/>
						</div>
					</div>
				</div>
			</div>
			<div class="row" style="${(parameter.type == '限时秒杀' ? 'display:none;' : '')}">
				<div class="col-xs-12">
					<div class="form-group">
						<label class="control-label col-sm-2"><span class="required ">*</span> ${text('素材')}：</label>
						<div class="col-sm-10">
							<div style="color:red;margin-bottom:10px;">
								建议尺寸：1125PX * 1125PX
							</div>
							<#form:hidden class="form-control" id="picture" name="picture" path="picture"/>
							<#form:fileupload id="imagepicture" uploadType="image" returnPath="true" filePathInputId="picture" class="form-control" readonly="false" maxUploadNum="255" imageMaxWidth="1125" imageMaxHeight="1125"/>
						</div>
					</div>
				</div>
			</div>
			<div class="row" style="${(parameter.type == '限时秒杀' ? 'display:none;' : '')}">
				<div class="col-xs-12">
					<div class="form-group">
						<label class="control-label col-sm-2" title="">
							<span class="required ">*</span>${text('活动说明')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-10">
							<#form:textarea path="introduction" rows="4" maxlength="255" class="form-control"/>
						</div>
					</div>
				</div>
			</div>
			<div class="row" style="${(parameter.type == '限时秒杀' ? 'display:none;' : '')}">
				<div class="col-xs-12">
					<div class="form-group">
						<label class="control-label col-sm-2" title="">
							${text('发货说明')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-10">
							<#form:input path="shipping" maxlength="255" class="form-control"/>
						</div>
					</div>
				</div>
			</div>
			<div class="row" style="${(parameter.type == '限时秒杀' ? 'display:none;' : '')}">
				<div class="col-xs-12">
					<div class="form-group">
						<label class="control-label col-sm-2" title="">
							${text('包邮说明')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-10">
							<#form:input path="freeShipping" maxlength="255" class="form-control"/>
						</div>
					</div>
				</div>
			</div>
			<div class="row">
				<div class="col-xs-12">
					<div class="form-group">
						<label class="control-label col-sm-2" title="">
							${text('朦胧文字')}：<i class="fa icon-question hide"></i></label>
						<div class="col-sm-10">
							<#form:input path="obscure" maxlength="255" class="form-control"/>
						</div>
					</div>
				</div>
			</div>
			<h4 class="form-unit">${text('活动商品表')}</h4>
			<div class="ml10 mr10">
				<table id="goodsSpecDataGrid"></table>
				<a href="javascript:void(0);" id="linkGoodsSpecDataGridAddRowBtn" class="btn btn-primary btn-sm mt10 mb10"><i class="fa fa-plus"></i> ${text('增行')}</a>
			</div>
		</div>
		<div class="box-footer">
			<div class="row">
				<div class="col-sm-offset-2 col-sm-10">
					<button type="submit" class="btn btn-sm btn-primary" id="btnSubmit"><i class="fa fa-check"></i> ${text('保 存')}</button>&nbsp;
					<button type="button" class="btn btn-sm btn-default" id="btnCancel" onclick="js.closeCurrentTabPage()"><i class="fa fa-reply-all"></i> ${text('关 闭')}</button>
				</div>
			</div>
		</div>
	</#form:form>
</div>
</div>
<% } %>
<script>
	var goodsIdList = [];
	var goodsSpecIdList = [];
	var selectData = [];

	$('#linkGoodsSpecDataGridAddRowBtn').click(function(){
		console.log(goodsSpecIdList);
		console.log(selectData);
		boxWidth = $(top.window).width() - 100,
				boxHeight = $(top.window).height() - 100;
		boxWidth = boxWidth < 350 ? 350 : boxWidth;
		boxHeight = boxHeight < 250 ? 250 : boxHeight;
		var options = {
			type: 2,
			maxmin: true,
			shadeClose: true,
			title: '选择商品',
			area: [boxWidth+'px', boxHeight+'px'],
			content: "${ctx}/storage/goods/select?checkbox=true&brandId="+$("#brandId option:selected").val(),
			// contentFormData: {
			// 	goodsIdList: goodsIdList,
			// 	selectData: js.encodeUrl(JSON.stringify(selectData))
			// },
			success: function(layero, index){
				if ($(js.layer.window).width() < boxWidth || $(js.layer.window).height() < boxHeight){
					js.layer.full(index);
				}
			},
			btn: ['<i class="fa fa-check"></i> 确定'],
			btn1: function(index, layero){
				var win = layero.iframeWindow();
				var selectData = win.getSelectData();
				console.log(selectData);

				// var ids = jQuery("#activityGoodsSpecDataGrid").jqGrid("getDataIDs");
				//获得当前最大行号（数据编号）
				// var maxRowId = ids.length === 0 ? 0 : Math.max.apply(Math,ids);
				//获得新添加行的行号（数据编号）
				// var newRowId = maxRowId;

				$.each(selectData,function (goodsIndex,goods) {
					$.each(goods.goodsSpecList,function (goodsSpecIndex,goodsSpec) {
						if($.inArray(goodsSpec.id,goodsSpecIdList) === -1){
							goodsSpecIdList.push(goodsSpec.id);
							var data = {
								id:goodsSpec.id,
								goodsId:goods.id,
								goodsSpecId:goodsSpec.id,
								code : goodsSpec.code ,
								name : goodsSpec.name,
								price:goodsSpec.price,
								memberPrice:goodsSpec.memberPrice,
								vipPrice:goodsSpec.vipPrice,
								agentPrice:goodsSpec.agentPrice,
								stockNum:goodsSpec.stockNum,
								saleNum:goodsSpec.saleNum
							};
							// newRowId = newRowId + 1;
							$("#goodsSpecDataGrid").jqGrid('addRowData', data.id, data ,'last');
							$('#goodsSpecDataGrid').jqGrid('editRow', data.id, false);
						}
					});
				});
			}
		};
		options.btn.push('<i class="fa fa-close"></i> 关闭');
		js.layer.open(options);
	});
	var columnModel = [
		{header:'主键', name:'id', editable:true, hidden:true},
		{header:'${text("状态")}', name:'status', editable:true, hidden:true},
		{header:'${text("商品id")}', name:'goodsId', editable:true, hidden:true},
		{header:'${text("商品规格id")}', name:'goodsSpecId', editable:true, hidden:true},
		{header:'${text("编码")}', name:'code', index:'a.code', width:150},
		{header:'${text("尺码")}', name:'name', index:'a.name', width:150},
		{header:'${text("吊牌价")}', name:'price', index:'a.price', width:150},
		{header:'${text("普通会员价")}', name:'memberPrice', index:'a.memberPrice', width:150},
		{header:'${text("VIP会员价")}', name:'vipPrice', index:'a.vipPrice', width:150},
		{header:'${text("代购费")}', name:'agentPrice', index:'a.agentPrice', width:150}
	];
	if("${parameter.type}"=== "限时秒杀"){
		columnModel.push({header:'${text("秒杀价")}', name:'seckillPrice', width:150, editable:true, edittype:'text', editoptions:{'class':'form-control number'}});
	}
	columnModel.push({header:'${text("库存")}', name:'stockNum', width:150, editable:true, edittype:'text', editoptions:{'maxlength':'11', 'class':'form-control required digits'}});
	columnModel.push({header:'${text("操作")}', name:'actions', width:80, sortable:false, fixed:true, formatter: function(val, obj, row, act){
			var actions = [];
			if (val == 'new'){
				actions.push('<a href="javascript:void(0);" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#goodsSpecDataGrid\').dataGrid(\'delRowData\',\''+obj.rowId+'\')});doRemove(\''+ (val || '')+'\',\''+obj.rowId+'\',\''+ (row.goodsId || '')+'\',\''+ (row.goodsSpecId || '') +'\');return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
			}else{
				actions.push('<a href="javascript:void(0);" onclick="js.confirm(\'${text("你确认要删除这条数据吗？")}\', function(){$(\'#goodsSpecDataGrid\').dataGrid(\'setRowData\',\''+obj.rowId+'\',null,{display:\'none\'});$(\'#'+obj.rowId+'_status\').val(\''+Global.STATUS_DELETE+'\');});doRemove(\''+ (val || '')+'\',\''+obj.rowId+'\',\''+ (row.goodsId || '')+'\',\''+ (row.goodsSpecId || '') +'\');return false;"><i class="fa fa-trash-o"></i></a>&nbsp;');
			}
			return actions.join('');
		}, editoptions: {defaultValue: 'new'}});

	//初始化活动商品表DataGrid对象
	$("#goodsSpecDataGrid").dataGrid({
		data: ${toJson(activity.goodsSpecList)},
		datatype: "local", // 设置本地数据
		autoGridHeight: function(){return 'auto'}, // 设置自动高度

		// 设置数据表格列
		columnModel: columnModel,

		// 编辑表格参数
		editGrid: true,				// 是否是编辑表格
		editGridInitRowNum: 0,		// 编辑表格的初始化新增行数
		editGridAddRowInitData: {id: '', status: Global.STATUS_NORMAL},	// 新增行的时候初始化的数据

		// 编辑表格的提交数据参数
		editGridInputFormListName: 'goodsSpecList', // 提交的数据列表名
		editGridInputFormListAttrs: 'status,id,goodsId,goodsSpecId,seckillPrice,stockNum', // 提交数据列表的属性字段

		// 加载成功后执行事件
		ajaxSuccess: function(data){
			goodsIdList = [];
			$.each(data.rows, function(key, value){
				goodsSpecIdList.push(value.goodsSpecId);
				if($.inArray(value.goodsId,goodsIdList) === -1){
					goodsIdList.push(value.goodsId);
				}
			});
		}
	});
	function doRemove(val,rowId,goodsId,goodsSpecId) {
		goodsSpecIdList.splice(goodsSpecIdList.indexOf(goodsSpecId) ,1);
	}
</script>
<script>
	$("#inputForm").validate({
		submitHandler: function(form){
			js.ajaxSubmitForm($(form), function(data){
				js.showMessage(data.message);
				if(data.result == Global.TRUE){
					js.closeCurrentTabPage(function(contentWindow){
						contentWindow.page();
					});
				}
			}, "json");
		}
	});
</script>